Optimization engine in a multi-tenant database system

ABSTRACT

More efficient querying of a multi-tenant database using dynamic tuning of database indices. A layer of meta-data associates data items with tenants, e.g., via tags, and the meta-data is used to optimize searches by channeling processing resources during a query to only those pieces of data bearing the relevant tenant&#39;s unique tag.

CROSS REFERENCES TO RELATED APPLICATIONS

The present application claims priority from and is a continuationapplication of U.S. Non-Provisional application Ser. No. 10/669,523(Attorney Docket No. 021735-00400US) filed on Sep. 23, 2003, the entirecontents of which are herein incorporated by reference for all purposes.

BACKGROUND OF THE INVENTION

The present invention, relates generally to database systems and moreparticularly to query optimization systems and methods for use inmulti-tenant database systems, wherein a centralized computer or set ofcomputing devices serve and store applications and data for use bymultiple tenants.

Multi-tenant database systems allow for users to access applicationsand/or data from a network source that, to the user, appears to becentralized (but might be distributed or backup, redundancy and/orperformance reasons). An example of a multi-tenant system is a computingsystem that is accessible to multiple independent parties to providethose parties with application execution and/or data storage. Wherethere is an appearance of centralization, and network access, eachsubscribing party (e.g., a “tenant”) can access the system to performapplication functions, including manipulating that tenant's data.

With a multi-tenant system, the tenants have the advantage that theyneed not install software, maintain backups, move data to laptops toprovide portability, etc. Rather, each tenant user need only be able toaccess the multi-tenant system to operate the applications and accessthat tenant's data. One such system usable for customer relationshipmanagement is the multi-tenant system accessible to salesforce.comsubscribers. With such systems, a user need only have access to a usersystem with network connectivity, such as a desktop computer withInternet access and a browser or other HHTTP client, or other suitableInternet client.

In database systems, to access, retrieve and process stored data, aquery is generated, automatically or manually, in accordance with theapplication program interface protocol for the database. In the case ofa relational database, the standard protocol is the structured querylanguage (SQL). SQL statements are used both for interactive queries fordata from the database and for gathering data and statistics. Theefficiency of the query method underlying the actual query is dependentin part on the size and complexity of the data structure scheme of thedatabase and in part on the query logic used.

Previous database query methods have been inefficient for multi-tenantdatabases because such methods do not understand, and fail to accountfor, the unique characteristics of each tenant's data. For example,while one tenant's data may include numerous short records having onlyone or two indexable fields, another tenant's data may include fewer,longer records having numerous indexable fields.

In addition to these structural (schema) differences, the distributionof data among different tenants may be quite different, even when theirschemas are similar. Modern relational databases rely onstatistics-based query optimizers that make decisions about the bestmanner to answer a query given accurate table-level and column-levelstatistics that are gathered periodically. Importantly, however, becauseexisting relational databases are not multi-tenant aware, thesestatistics cut across all tenants in the database. That is, thestatistics that are gathered are not specific to any one tenant, but arein fact an aggregate or average of all tenants. This approach can leadto incorrect assumptions and query plans about any one tenant.

As a specific example, Oracle provides a query optimizer that can beused on an Oracle database. This query optimizer works generally asfollows: for each table, column, or index, aggregate statistics aregathered (typically periodically or on demand by a databaseadministrator (“DBA”)). The gathered statistics typically include thetotal number of rows, average size of rows, total number of distinctvalues in a column or index (an index can span multiple columnshistograms of column values (which place a range of values intobuckets), etc. The optimizer then uses these statistics to decide amonga possible set of data access paths.

In general, one goal of a query optimizer is to minimize the amount ofdata that must be read from disk (e.g., because disk access may be aslow operation). The optimizer therefore typically chooses tables orcolumns that are most “selective”—that is, will yield the fewest rowswhen the query condition is evaluated. For instance, if a single queryfilters on two columns of a single table, and both columns are indexed,then the optimizer will use the index that has the highest number ofdistinct values because statistically for any given filter value asmaller number of rows are expected to be returned. If the optimizerknows that a certain column has a very high cardinality (number ofdistinct values) then the optimizer will choose to use an index on thatcolumn versus a similar index on a lower cardinality column. Theoptimizer assumes relatively even distribution of data and thereforereaches the conclusion that the high-cardinality column is likely toyield a smaller number of satisfying-rows for a given equality filter.

Now consider in a multi-tenant system a physical column (shared by manytenants) that has a large number of distinct values for most tenants,but a small number of distinct values for a specific tenant. For thislatter tenant the query optimizer will use this overall-high-cardinalitycolumn in error—because the optimizer is unaware that for this specifictenant the column is not selective.

In the case of table joins, the optimizer's decisions may be even moreimportant—deciding which table to retrieve first can have a profoundimpact on overall query performance. Here again, by using system-wideaggregate statistics the optimizer might choose a query plan that isincorrect or inefficient for a single tenant that does not conform tothe “normal” average of the entire database as determined from thegathered statistics.

Accordingly, it is desirable to provide systems and methods foroptimizing database queries, and for dynamically tuning a queryoptimizer, in a multi-tenant database system which overcome the aboveand other problems.

BRIEF SUMMARY OF THE INVENTION

The present invention provides methods and systems for optimizingdatabase queries in a multi-tenant database system. In certain aspects,for example, the present invention provides methods for dynamicallyturning a query optimizer based on particular data characteristics ofthe tenant whose data is being searched and the particular query beingexecuted.

The systems and methods of the present invention advantageously harnessgreater semantic knowledge about the use of data tables by theunderlying relational database. By tracking tenant-level statistics ontop of the typical system-gathered statistics (e.g., Oracle-gatheredstatistics), the pre sent invention is advantageously able to optimizequeries and/or make recommendations to the underlying query optimizer toimprove its knowledge of the data, and therefore increase systemperformance, particularly for individual tenants.

According to the present invention, a multi-tenant database stores datafrom multiple tenants. While the overall database structure or schema isfixed, each tenant may have a tenant-specific virtual schema thatdescribes the logical structure of that tenant's data. In certainaspects, each tenants virtual schema includes a variety of customizablefields, some or all of which may be designated as index able.

According to an aspect of the present invention, a method is providedfor optimizing a query in a multi-tenant database having one or moredata tables, each table having one or more logical columns defining datacategories and one or more logical rows associated with one or moretenants, wherein a plurality of tenants have data stored in the datatables. The method typically includes generating tenant-level statisticsfor each of said plurality of tenants for each of the data tables,receiving a SQL query, and optimizing the SQL query based on thetenant-level statistics. In certain aspects, the method also includesgenerating user-level statistics for each user of each tenant andoptimizing the SQL query based on the user-level statistics.

According to another aspect of the present invention, a multi-tenantdatabase system is provided. The multi-tenant database system typicallyincludes a database having one or more data tables, each table havingone or more columns defining data categories and one or more rowsassociated with one or more tenants, wherein a plurality of tenants havedata stored in the data tables. The database system also typicallyincludes a statistics generating module configured to generatetenant-level statistics for each tenant for each of the data tables, anda query optimization module, configured to optimize a database querybased on the tenant-level statistics. In certain aspects, the statisticsgeneration engine is configured to generate user-level statistics foreach user of each tenant, and the query optimization module isconfigured to optimize a database query based on the user-levelstatistics.

Reference to the remaining portions of the specification, including thedrawings and claims, will realize other features and advantages of thepresent invention. Further features and advantages of the presentinvention, as well as the structure and operation of various embodimentsof the present invention, are described in detail below with respect tothe accompanying drawings. In the drawings, like reference numbersindicate identical or functionally similar elements.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 illustrates an environment wherein a multi-tenant database system(MTS) might be used according to one embodiment.

FIG. 2 illustrates elements of an MTS and interconnections therein inmore detail according to one embodiment.

FIG. 3 illustrates an example of a data model for sharing.

DETAILED DESCRIPTION OF THE INVENTION

FIG. 1 illustrates an environment wherein a multi-tenant database systemmight be used. As illustrated in FIG. 1 (and in more detail in FIG. 2)any user systems 12 might interact via a network 14 with a multi-tenantdatabase system (MTS) 16. The users of those user systems 12 might beusers in differing capacities and the capacity of a particular usersystem 12 might be entirely determined by the cur ent user. For example,where a salesperson is using a particular user system 12 to interactwith MTS 16, that user system has the capacities allotted to thatsalesperson. However, while an administrator is using that user systemto interact with MTS 16, it has the capacities allotted to thatadministrator.

Network 14 can be a LAN (local area network, WAN (wide area network,wireless network, point-to-point network, star network, token ringnetwork, hub network, or other configuration. As the most common type ofnetwork in current use is a TCP/IP (Transfer Control Protocol andInternet Protocol) network such as the global internetwork of networksoften referred to as the “Internet” with a capital “,” that will be usedin many of the examples herein, but it should be understood that thenetworks that the present invention might use are not so limited,although TCP/IP is the currently preferred protocol.

User systems 12 might communicate with MTS 16 using TCP/IP and, at ahigher network level, use other common Internet protocols tocommunicate, such as HTTP, FTP, AFS, WAP, etc. As an example, where HTTPis used, user system 12 might include an HTTP client commonly referredto as a “browser” for sending and receiving HTTP messages from an HTTPserver at MTS 16. Such HTTP server might be implemented as the solenetwork interface between MTS 16 and network 14, but other techniquesmight be used as well or instead. In some implementations, the interfacebetween MTS 16 and network 14 includes load sharing functionality, suchas round-robin HTTP request distributors to balance loads and distributeincoming HTTP requests evenly over a plurality of servers. Preferably,each of the plurality of servers has access to the MTS's data, at leastas for the users that are accessing a server.

In preferred aspects, the system shown in FIG. 1 implements a web-basedcustomer relationship management (CRM) system. For example, in oneaspect, MTS 16 can include application servers configured to implementand execute CRM software applications as well as provide related data,code, forms, web pages and other information to and from user systems 12and to store to, and retrieve from, a database system related data,objects and web page content. With a multi-tenant system, tenant data ispreferably arranged so that data of one tenant is kept separate fromthat of other tenants so that that one tenant does not have access toanother tenant's data, unless such data is expressly shared.

One arrangement for elements of MTS 16 is shown in FIG. 1, including anetwork interface 20, storage 22 for tenant data, storage 24 for systemdata accessible to MTS 16 and possibly multiple tenants, program code 26for implementing various functions of MTS 16, and a process space 28 forexecuting MTS system processes and tenant-specific processes, such asrunning applications as part of an application service.

Several elements in the system shown in FIG. 1 include conventional,well-known elements that need not be explained in detail here. Forexample, each user system 12 could include a desktop personal computer,workstation, laptop, PDA, cell phone, or any WAP-enabled device or anyother computing device capable of interfacing directly or indirectly tothe Internet or other network connection. User system 12 typically runsan HTTP client, e.g., a browsing program, such as Microsoft's InternetExplorer™ browser, Netscape's Navigator™ browser, Opera's browser, or aWAP-enabled browser in the case of a cell phone, PDA or other wirelessdevice, or the like, allowing a user (e.g., subscriber of a CRM system)of user system 12 to access, process and view information and pagesavailable to it from MTS 16 over network 14. Each user system 12 alsotypically includes one or more user interface devices, such as akeyboard, a mouse, touch screen, pen or the like, for interacting with agraphical user interface (GUI) provided by the browser on a display(e.g., monitor screen, LCD display, etc.) in conjunction with pages,forms and other information provided by MTS 16 or other systems orservers. As discussed above, the present invention is suitable for usewith the Internet, which refers to a specific global internetwork ofnetworks. However, it should be understood that other networks can beused instead of the Internet, such as an intranet, an extranet, avirtual private network (VPN), a non-TCP/IP based network, any LAN orWAN or the like.

According to one embodiment, each user system 12 and all of itscomponents are operator configurable using applications, such as abrowser, including computer code run using a central processing unitsuch as an Intel Pentium processor or the like. Similarly, MTS 16 (andadditional instances of MTS's, where more than one is present) and allof their components might be operator configurable using application(s)including computer code run using a central processing unit such as anIntel Pentium processor or the like, or multiple processor units.Computer code for operating and configuring MTS 16 to intercommunicateand to process web pages and other data and media content as describedherein is preferably downloaded and stored on a hard disk, but theentire program code, or portions thereof, may also be stored in anyother volatile or non-volatile memory medium or device as is well known,such as a ROM or RAM, or provided on any media capable of storingprogram code, such as a compact disk (CD) medium, digital versatile disk(DVD) medium, a floppy disk, and the like. Additionally, the entireprogram code, or portions thereof, may be transmitted and downloadedfrom a software source, e.g., over the Intermet, or from another server,as is well known, or transmitted over any other conventional networkconnection as is well known (e.g., extranet, VPN, LAN, etc.) using anycommunication medium and protocols (e.g., TCP/IP, HTTP, HTTPS, Ethernet,etc) as are well known. It will also be appreciated that computer codefor implementing aspects of the present invention can be implemented inany programming language that can be executed on a server or serversystem such as, for example, in C, C+, HTML, Java, JavaScript, or anyother scripting language, such as VBScript.

According to one embodiment, each MTS 16 is configured to provide webpages, forms, data and media content to user systems 12 to support theaccess by user systems 12 as tenants of MTS 16. As such, MTS 16 providessecurity mechanisms to keep each tenant's data separate unless the datais shared. If more than one MTS is used, they may be located in closeproximity to one another (e.g., in a server farm located in a singlebuilding or campus), or they may be distributed at locations remote fromone another (e.g., one or more servers located in city A and one or moreservers located in city B). As used herein, MTS's could include one ormore logically and/or physically connected servers distributed locallyor across one or more geographic locations. Additionally, the term“server” is meant to include a computer system, including processinghardware and process space(s), and an associated storage system anddatabase application as is well known in the art. It should also beunderstood that “server system” and “server” are often usedinterchangeably herein. Similarly, the databases described herein can beimplemented as single databases, a distributed database, a collection ofdistributed databases, a database with redundant online or offlinebackups or other redundancies, etc and might include a distributeddatabase or storage network and associated processing intelligence.

FIG. 2 illustrates elements of MTS 16 and various interconnections inmore detail. In this example, the network interface is implemented asone or more HTTP application servers 100. Also shown is system processspace 102 including individual tenant process spaces 104, a systemdatabase 106, tenant database(s) 108 and a tenant management processspace 110. Tenant database 108 might be divided into individual tenantstorage areas 112, which can be either a physical arrangement or alogical arrangement. Within each tenant storage area 112, user storage114 might be allocated for each user.

It should also be understood that each application server 100 may becommunicably coupled to database systems, e.g., system database 106 andtenant database(s) 108, via a different network connection. For example,one server 100 might be coupled via the Internet 14, another server 100_(N-1) might be coupled via a direct network link, and another server100 _(N) might be coupled by yet a different network connection.Transfer Control Protocol and Internet Protocol (TCP/IP) are prepreferred protocols for communicating between servers 100 and thedatabase system, however, it will be apparent to one skilled in the artthat other transport protocols may be used to optimize the systemdepending on the network interconnect used.

In preferred aspects, each application server 100 is configured tohandle requests for any user/organization. Because it is desirable to beable to add and remove application servers from the server pool at anytime for any reason, there is preferably no Sever affinity for a userand/or organization to a specific application server 100. In oneembodiment, therefore, an interface system (not shown) implementing aload balancing function (e.g., an F5 Big-IP load balancer) iscommunicably coupled between the servers 100 and the user systems 12 todistribute requests to the servers 100. In one aspect, the load balanceruses a least connections algorithm to route user requests to the servers100. Other examples of load balancing algorithms, such as are roundrobin and observed response time, also can be used. For example, incertain aspects, three consecutive requests from the same user could hitthree different servers, and three requests from different users couldhit the same server. In this manner, MTS 16 is multi-tenant, wherein theMTS 16 handles storage of different objects and data across disparateusers and organizations.

As an example of storage, one tenant might be a company that employs asales force where each salesperson uses MTS 16 to manage their salesprocess. Thus, a user might maintain contact data, leads data, customerfollow-up data, performance data, goals and progress data, allapplicable to that user's personal sales process (e.g., in tenantdatabase 108). In the preferred MTS arrangement, since all of this dataand the applications to access, view, modify, report, transmit,calculate, et, can be maintained a ad accessed by a user system havingnothing more than network access, the user can manage his or her salesefforts and cycles from any of many different user systems. For example,if a salesperson is paying a visit to a customer and the customer hasInternet access in their lobby, the salesperson can obtain criticalupdates as to that customer while waiting for the customer to arrive inthe lobby.

While each user's sales data might be separate from other users' salesdata regardless of the employers of each user, some data might beorganization-wide data shared or accessible by a plurality or all of thesales force for a given organization that is a tenant. Thus, there mightbe some data structures managed by MTS 16 that are allocated at thetenant level while other data structures are managed at the user level.Because an MTS might support multiple tenants including possiblecompetitors, the MTS should have security protocols that keep data,applications and application use separate. Also, because many tenantswill opt for access to an MTS rather than maintain their own system,redundancy, up-time and backup are more critical functions and need tobe implemented in the MTS.

In addition to user-specific data and tenant-specific data, MTS 16 mightalso maintain system level data usable by multiple tenants. Such systemlevel data might include industry reports, news, postings, and the likethat are sharable among tenants.

In certain aspects, client systems 12 communicate with applicationservers 100 to request and update system-level and tenant-level datafrom MTS 16 that may require one or more queries to database system 106and/or database system 108. MTS 16 generates automatically one or moreSQL statements (the SQL query) designed to access the desiredinformation.

Each database can generally be viewed as a set of logical tablescontaining data fitted into predefined categories. Each table generallycontains one or more data categories logically arranged in physicalcolumns. Each row of a table contains an instance of data for eachcategory defined by the columns. For example, a CRM database may includea table that describes a customer with columns for basic contactinformation such as name, address, phone number, fax number, etc.Another table might describe a purchase order, including columns forinformation such as customer, product, sale price, date, etc.

Now, consider in a multi-tenant system a physical column (shared by manytenants) that has a large number of distinct values for most tenants,but a small number of distinct values for a specific tenant. For thislatter tenant, a typical database optimizer will choose to use thisoverall-high-cardinality column in error because the optimizer isunaware that for this specific tenant the column is not selective.

In the case of table joins the optimizer's decisions may be even moreimportant—deciding which table to retrieve first can have a profoundimpact on overall query performance. Here again, by using system-wideaggregate statistics the optimizer might choose a query plan that isincorrect or inefficient for a single tenant that does not conform tothe “normal” average of the entire database.

As a specific example of the importance of table joins, consider thesharing feature in the salesforce.com service. The sharing featureallows a specific list of users to have access to privileged data, suchas specific accounts or opportunities. In one aspect, a Many-to-Many(MTM) physical table serves as the storage for this sharing information.The MTM table specifies that a user as access to a particular entity(e.g., account or opportunity) row. When displaying a list of all rowsthat the current user can see (possibly with a filter on the entityrows, such as the name of the account or the dollar amount of theopportunity) the query optimizer must choose between accessing this MTMtable from the user or the entity side of the relationship. If theentity filter is highly selective (for instance, a particular accountname such as “XYZ Corp”) it will generally make more sense to begin thequery access path from this side. If, however, the entity is notfiltered selectively, but the current user has access to a small amountof data, then the query optimizer should access rows in the MTM tablethrough the user side of this relationship.

However, in the above example in a multi-tenant database system theoptimizer's native statistics may be insufficient to make thisdetermination because the native statistics will aggregate across toomany tenants and will not have context into the current tenant's data.Note, because of the wide range of business types, industries, and sizespotentially served by multi-tenant database systems such as thesalesforce.com service, the likelihood of data “skew” is greatlyincreased. That is, the statistical profile of the largest most complextenants is likely to look very different from that of small or mediumsized customers.

In Oracle database systems, override mechanisms are provided to affectthe Oracle automatic query optimizer. The use of query “Hints” allowsthe SQL author the ability to choose explicitly a query plan. Forinstance, a human-authored SQL statement might mention the explicitorder of table joins, or explicit index names to use (rather thanletting the optimizer choose automatically). Another mechanism forcontrolling the query plan explicitly is to re-write the query usingdifferent SQL syntax. For instance, a single flat SQL statement can bere-written using a nested SELECT in the FROM clause of the outer query.Joins and semi-joins are sometimes interchangeable. Anti-joins can bewritten using the MINUS operator, etc. All of these are examples of waysin which a human-author, or a programmatic SQL generator, can alter thebehavior of the underlying query optimizer by using higher-levelknowledge to change the plan.

In certain aspects, the present invention configures or tunes a queryoptimizer, such as the Oracle query optimizer, by supplying appropriate“hints.” For example, when SQL is generated programmatically by the MTS,the tenant-level statistics are consulted and a dynamic decision is madeas to the syntax of the query. The tenant-level statistics preferablymirror the underlying relational database statistics in many ways (forexample, in one aspect they track the total number of distinct valuesfor indexed columns) but the statistics are kept on a permanent basis(e.g., in tables in tenant database storage areas 112). Similarly forimportant application functionality, such as the sharing feature, theMTS tracks the approximate number of rows to which each user has accessand stores such statistics (e.g., tables stored in user storage areas114 of database 108). Then, when a filtered sharing query arrives, thedynamically generated SQL includes the appropriate hints and structureto force a query plan that is optimal.

Optimizer

In one aspect, metadata information about users andtenants/organizations and the data contained in entity rows for thattenant are tracked (e.g., relevant information and metadata stored toseparate user-level and tenant-level data tables) in order to makechoices about query access paths, particularly for list-style queriessuch as reports. The areas targeted include:

-   -   1. The evaluation of a sharing model, which controls which users        can see which records. The optimizer preferably distinguishes        between users that can see many rows in, an organization (e.g.,        bosses) versus users who can see very few rows (e.g., lower        level employees).    -   2. The choice of which filters are the most selective for fields        that contain enumerated lists of values (e.g., list of status        values for an account, list of industries, list of states,        etc.).

Sharing Model

For each user in the system a approximate count of the number of rows(for each entity type that has a sharing model) that the user can see istracked. This number of rows (as a percentage of the total number ofentity rows for that organization) is used as a decision point inchoosing between two different query paths. It has been determinedempirically that users who can see most of the entity rows (e.g. bosses)benefit from a certain query structure, whereas users who can see asmall percentage of the entity rows (e.g., lower level employees)benefit from a different query structure. Current systems are not ableto choose between these paths without having an entirely different SQLprovided via a programmatic decision. In preferred aspects, anoptimization engine reads data from multi-tenant data tables and storesmetadata (e.g., number of rows accessible permanent or per user, orother metadata) to tenant-level tables or user-level tables in database108. For example, a tenant-level metadata table might be stored to atenant, storage area 112 and a user-level table might be stored to auser storage area 114. For example, in one aspect, the optimizationengine includes a statistics generation engine that process multi-tenanttables and produces tenant-level and user-level statistics tables. Theoptimization engine and statistics generation engine might execute inprocess space 110 or other process space. The optimization engineretrieves and processes the appropriate tables when optimizing SQLqueries. In other aspects, flags or tags are implemented in themulti-tenant database tables to distinguish users and tenants.

In order to keep the statistics up to date it is important to track thepercentage of rows that each and every user can see. In one aspect,there are three ways in which a user might gain access to data in aprivate security model:

(1) Rows owned by the user or users below him in the role hierarchy;

(2) Rows that are shared via sharing rules to a group to which this userbelongs; and

(3) Rows that are shared via manual/team sharing to this user (possiblyvia a group).

In a preferred aspect, statistics and metadata are tracked for user andorganization quotas. In some aspects, such information is trackedperiodically (e.g., on a scheduled basis during off peak hours,amortizing the work over multiple days), wherein the number of visiblerows for each user is calculated exactly or approximately, or beforeevery Nth query (e.g., every 10^(th) query) by a user, that user'svisibility is calculated explicitly and then that statistic is useduntil it is again calculated (here it is assumed that users do notchange very often from one strategy to another). In yet a furtheraspect, whenever an unconstrained query is run, the number of visiblerows is remembered and that number is used until the user runs the nextunconstrained query.

In one aspect, the total number of rows for each entity type for eachorganization is tracked (this is useful for any of the strategiesabove). Also, the total number of rows owned by each user in a metadatatable is tracked.

If it is assumed that (1) and (2) are the most important reasons for whya user has access to entity records (this might be known empiricallyfrom how organizations use the system) then the information needed tocalculate the number of rows a user can see, approximately, is known.Namely, the role hierarchy metadata tables can be used in conjunctionwith the metadata table to determine the number of records owned by theuser or his subordinates. The sharing rule metadata can also be usedalong with the group definition metadata to calculate the total numberof rows visible via sharing rules. While these two sets may overlap, forthe purpose of the heuristic decision between “boss” and “lower levelemployee,” the sum of these two values is sufficiently close to the truevalue.

In one aspect, the use of metadata tables only which are generally muchsmaller than the actual entity tables which might have millions of rows)ensures that the calculation of visible rows will itself not requiremuch time. In one aspect, this decision is cached in a user-informationdata structure kept by the running application servers 100, e.g., with atimeout value. In this manner, even though the calculation itself may berelatively lightweight, it is only performed periodically while a useris logged in.

To focus on how the “boss” vs “lower level employee” decision shoulddrive an appropriate query plan, consider a query of the for: “Show meall accounts that can see” in a private account sharing model. Anexample of a data model for sharing appears in FIG. 3 (middle table issharing table, final table is the user/group “blowout” table whichdescribes which users are contained in a group, or above a user in therole hierarchy (UG=User or Group). According to one aspect, for a “lowerlevel employee” user it is typically most advantageous to join thesetables starting from the right, filtering on users Id to form atemporary result of the rows that can be seen. Because the user can notsee many rows, this will yield a relatively selective path. An examplequery follows:

  select a.name “ACCOUNT.NAME”, from sales.account a, (select distincts.account_id  from core.ug_blowout b, sales.acc_share s    wheres.organization_id = ?    and b.organization_id = ?    and b.users_id = ?   and s.ug_id = b.ug_id    and s.acc_access_level > 0) t,   core.usersu where (t.account_id = a.account_id) and (u.users_id = a.owner) and(a.deleted = ‘0’) and (a.organization_id = ?) and (u.organization_id =?))

Conversely for a “boss” user who can see most of the entity records inthe organization, it is typically most advantageous to begin the queryfrom the left and use a nested loop query plan onto the sharing table(ace share), an example of which follows:

  select  a.name “ACCOUNT.NAME”, from  sales.account a, core.users uwhere (u.users_id = a.owner) and (a.deleted = ‘0’) and(a.organization_id = ?) and (exists (select 1   from core.ug_blowout b,   sales.acc_share s  where s.organization_id = ?  and b.organization_id= ?  and b.users_id = ?  and s.ug_id = b.ug_id  and s.acc_access_level >0  and s.account_id = a.account_id)) and (u.organization_id = ?)

Note that this query in general runs in relatively constant (reasonable)time for all users in an organization. It may not be particularly fastsince it must look at all top-level entity records, but it is suitablefor a boss who can in fact see most records. The first “lower levelemployee” query runs much faster for users who in fact can not see manyrecords, but it may run much slower for bosses who can see all records.This, again, is why it is desirable to have an accurate decision betweenthe two paths.

Filter Choice

A typical end user report execution includes a set of displayed columnsfrom multiple tables along with a set of filter conditions. A typicalreport might join between 3 and 7 (or more) main tables with filteringpossibly occurring on one or more of these tables. In addition, certainfilters, such as the sharing filter discussed above (which can take theform of an additional join or a nested sub-query), should be applied toassure that the end user only sees data to which he has been givenaccess.

Information about enumerated “picklist” fields (those fields that areknown to contain a small list of possible values) are tracked in oneaspect. Examples of these fields include the list of priority values fora task and the list of industries for an account. These fields are oftenused as filters for executive reporting and data rollup reports. Inaddition to the values themselves, the approximate number of times eachvalue appears in the actual entity table for that organization (tenant)is tracked in the metadata. When a user provides a filter value suchthat the value appears infrequently for that organization, the overallquery is preferably driven from that table and possibly from an index onthat column, if such as index exists.

In one aspect, when a user runs a report with N filters, each filter isevaluated for expected selectiveness. If, for example, the user filterson “Caifornia” and “Florida” from a list of states and it is known thatthese values represent, receptively, 5 and 2 percent of the overallrows, then it is assumed that the filter has a 7% selectivity. Similarlyif a boolean field has 95% true values, then filtering on false appearsattractive as a filter, whereas filtering on Male from a random list ofpeople would not be very selective, since 50% reduction would not make agood filter condition.

The selectivity of the sharing condition is also considered in oneaspect. For a user with very low (perhaps 2%) visibility, the sharingfilter might prove to be the best starting point and therefore theoptimizer is instructed to begin with the filter, rather than one of themain entity tables such as, e.g., Account or Contact tables.

The cost-based optimizer, in one aspect, incorporates other filtertypes, in addition to semantics knowledge about the application. Forexample, if an organization has imported all opportunities for the last3 years, and a user report filters on “all opportunities that closed inthe last week” then this is likely to represent a selective filter. Thepresence of custom field indexes (e.g., a certain set of columns thatadministrators can choose to place into a B-tree indexed custom fieldcolumns into these heuristic decisions) acre also factored in oneaspect.

In one aspect, a query is only hinted if it is assumed that a particularstarting table will yield a selective path. All other tables would thenbe joined via nested loops, Note, these types of cost-based decisionsare similar to the decisions that the optimizer (e.g., Oracle optimizer)itself makes when deciding how to join tables. Importantly; the systemof the present invention makes tenant-level data decisions based ontenant-level statistics and user-level data decisions based onuser-level statistics. The system of the present invention also takesinto account application-level concepts such as sharing that are beyondthe generic nature of the underlying RBDMS.

For picklist fields, the statistics tracked and stored do not need toreflect the exact number of occurrences for each value, a reasonableestimate is sufficient in one aspect. Values missing from the statisticseither do not occur at all, or occur infrequently—it is assumed thatthey make good filters.

In one embodiment, each update or insert into an entity table passesthrough an application server 100. Therefore as the data is beingprocessed in Java counters are incremented and decremented forindividual picklist values. Inserts increment a counter, while updatesto a different value decrement a counter for the old value and incrementa counter for the new value. Since these statistics do not need to beexact, the statistics metadata is preferably not updated with each andevery database insert or update (which might affect performance).Rather, in one aspect, an in-memory cache server (which already containsthe metadata for valid picklist values) is augmented with the countersfor these values, with the ability to update the database valuesperiodically to persist the changes. An example of such a cache servercan be found in U.S. patent application Ser. No. 10/418,961, filed Apr.17, 2003, titled “Java Object Cache Server for Databases”, the contentsof which are hereby incorporated by reference in its entirety.

For row deletion, the data preferably does not pass through theapplication server 100. However, all main entities are preferablysot-deleted (with a modification stamp), meaning the rows are left inthe database for possible un-deletion. Therefore, an asynchronousprocess is preferably used to update the statistics when rows aredeleted and un-deleted since it is known which rows have been touchedsince the last running of that process.

While the invention has been described by way of example and in terms ofthe specific embodiments, it is to be understood that the invention isnot limited to the disclosed embodiments. To the contrary, it isintended to cover various modifications and similar arrangements aswould be apparent to those skilled in the art. Therefore, the scope ofthe appended claims should be accorded the broadest interpretation so asto encompass all such modifications and similar arrangements.

1. A system, comprising: a customer relationship management databasehaving one or more data tables; and an optimization engine which readsdata from at least one of the one or more data tables and which storesthe data in the customer relationship management database; wherein thedata is tracked in order to gather statistical information to makechoices about query access paths.
 2. The system of claim 1, wherein thestatistical information includes a number of rows to which a user hasaccess.
 3. The system of claim 1, wherein the customer relationshipmanagement database structure is fixed.
 4. The system of claim 1,wherein the system is a multi-tenant database system.
 5. The system ofclaim 4, wherein the multi-tenant database system is configured to keeptenant's data separate unless the data is shared.
 6. The system of claim1, wherein the customer relationship management database contains aplurality of customizable fields.
 7. A computer program product,comprising: computer code for reading data from one or more data tablesof a customer relationship management database; and computer code forstoring the data in the customer relationship management database;wherein the computer program product is operable such that the data istracked in order to gather statistical information to make choices aboutquery access paths.
 8. The computer program product of claim 7, whereinthe statistical information includes a number of rows to which a userhas access.
 9. The computer program product of claim 7, wherein thecustomer relationship management database structure is fixed.
 10. Thecomputer program product of claim 7, wherein the computer programproduct is implemented in a multi-tenant database system.
 11. Thecomputer program product of claim 10, wherein the multi-tenant databasesystem is configured to keep tenant's data separate unless the data isshared.
 12. The computer program product of claim 7, wherein thecustomer relationship management database contains a plurality ofcustomizable fields.
 13. A method, comprising: reading data from one ormore data tables of a customer relationship management database; andstoring the data in the customer relationship management database;wherein the computer program product is operable such that the data istracked in order to gather statistical information to make choices aboutquery access paths.
 14. The method of claim 13, wherein the statisticalinformation includes a number of rows to which a user has access. 15.The method of claim 13, wherein the customer relationship managementdatabase structure is fixed.
 16. The method of claim 13, wherein thecomputer program product is implemented in a multi-tenant databasesystem.
 17. The method of claim 16, wherein the multi-tenant databasesystem is configured to keep tenant's data separate unless the data isshared.
 18. The method of claim 13, wherein the customer relationshipmanagement database contains a plurality of customizable fields.